<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="confirm"
      @cancel="$emit('close')"
    />
  </div>
</template>

<script>
import { modefyUserProfile } from "@/api/login";
import dayJs from "dayjs";
export default {
  props: ["value"],
  data() {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value), //用于绑定和修改当前的日期
    };
  },
  methods: {
    confirm() {
      this.modefyNameFn();
    },
    async modefyNameFn() {
      this.$toast.loading({
        message: "修改中...",
        forbidClick: true,
        duration: 0,
      });
      try {
        await modefyUserProfile({
          birthday: dayJs(this.currentDate).format("YYYY-MM-DD"),
        });
        this.$emit("input", dayJs(this.currentDate).format("YYYY-MM-DD"));
        this.$emit("close");
        this.$toast("修改成功");
      } catch (error) {
        console.log(error);
        this.$toast("修改失败");
      }
    },
  },
};
</script>

<style>
</style>